//
//  SCAllClassifyView.m
//  shangcheng
//
//  Created by 张艳江 on 2019/6/16.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCAllClassifyView.h"
#import "SCAllClassifyCell.h"

@interface SCAllClassifyView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSArray          *dataArray;

@end

@implementation SCAllClassifyView

- (instancetype)initWithUrl:(NSString *)url{
    
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, kTopHeight + 50, kScreenW, kScreenH - kTopHeight - 50);
        self.backgroundColor = [UIColor whiteColor];
        self.alpha = 0;
        [self setUI];
        //请求数据
        [self requestData:url];
        
        [ApplicationDelegate.window addSubview:self];
    }
    return self;
}
- (void)setUI{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat w = (kScreenW - 41)/3.0f;
    layout.itemSize = CGSizeMake(w, 35);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-kTopHeight) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerNib:[UINib nibWithNibName:@"SCAllClassifyCell" bundle:nil] forCellWithReuseIdentifier:@"SCAllClassifyCellID"];
    [self addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SCAllClassifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCAllClassifyCellID" forIndexPath:indexPath];
    SCModel *model = self.dataArray[indexPath.row];
    cell.titleLab.text = model.cat_name;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self dismiss];
    if ([self.delegate respondsToSelector:@selector(clickClassifyWithModel:)]) {
        [self.delegate clickClassifyWithModel:self.dataArray[indexPath.row]];
    }
}
#pragma mark - 请求数据
- (void)requestData:(NSString *)urlStr{
    
    NSString *url = [MyTool splicingWithUrl:urlStr];
    [[NetworkRequestTool sharedNetworkTool] GET:url parameters:nil finished:^(id result, NSString *error) {
        //NSLog(@"---%@",result);
        self.dataArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
        [self.collectionView reloadData];
    }];
}
#pragma mark -- showPickerView
- (void)show{
    
    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = 1;
    }];
}
- (void)dismiss{
    
    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = 0;
    }];
}

@end
